package cc.web.vaaking.servlet.vacation;

import cc.web.vaaking.common.*;
import cc.web.vaaking.common.utils.ValueUtils;
import cc.web.vaaking.entity.SysVacationRecord;
import cc.web.vaaking.services.SysVacationRecordService;
import cc.web.vaaking.servlet.BaseHttpServlet;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/apply/*")
public class ApplicationServlet extends BaseHttpServlet {
    SysVacationRecordService sysVacationRecordService = new SysVacationRecordService();

    @Override
    public void init() throws ServletException {
        routerMap.put("applicationPage", this::applyPage);
        routerMap.put("appListPage", this::appListPage); // list jsp界面
        routerMap.put("addOne", this::addOne);
        routerMap.put("getAppList", this::getAppList);
    }


    private void getAppList(HttpServletRequest request, HttpServletResponse response) {
        int userid = Integer.parseInt(ValueUtils.getParamOrDefault(request, "userId", "1"));
        try {
            List<SysVacationRecord> list = sysVacationRecordService.selectAllByUserId(userid);
            PageInfo<SysVacationRecord> info = new PageInfo<>(list);
            AjaxTool.returnAjax(response, AjaxResult.success(info));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }

    }

    private void appListPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            int userId =Integer.parseInt(ValueUtils.getParamOrDefault(request,"userId", String.valueOf(LoginContext.getCurrentUser().getId())));
            request.setAttribute("userId",userId);
            request.getRequestDispatcher("../views/apply/appList.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addOne(HttpServletRequest request, HttpServletResponse response) {
        SysVacationRecord sysVacationRecord = AjaxTool.getJSON(request).toJavaObject(SysVacationRecord.class);
        int userId = LoginContext.getCurrentUser().getSysUser().getId();
        if (sysVacationRecordService.insert(userId, sysVacationRecord) == 1) {
            AjaxTool.returnAjax(response, AjaxResult.success("申请成功"));
        } else {
            AjaxTool.returnAjax(response, AjaxResult.error("申请失败"));
        }
    }

    private void applyPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("../views/apply/application.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
